package me.firstapp.service.weixin.member.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.weibi.WeiBiAccount;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace.ChangeType;
import me.firstapp.repository.weixin.jinbi.JinBiAccountRepository;
import me.firstapp.repository.weixin.member.MemberRepository;
import me.firstapp.repository.weixin.weibi.WeiBiAccountRepository;
import me.firstapp.repository.weixin.weibi.WeiBiAccountTraceRepository;
import me.firstapp.service.weixin.member.MemberService;

@Repository
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private WeiBiAccountRepository weiBiAccountRepository;

	@Autowired
	private WeiBiAccountTraceRepository weiBiAccountTraceRepository;

	@Autowired
	private JinBiAccountRepository jinBiAccountRepository;

	@Override
	public Member findMemberByOpenId(String openId) {
		return memberRepository.findMemberByOpenId(openId);
	}

	@Override
	public Long findTotalFansCounts(String chiefOpenId) {
		Long total = memberRepository.findTotalFansCounts(chiefOpenId);
		if (null == total) {
			total = 0L;
		}
		return total;
	}

	@Override
	public List<Member> findFansPage(Integer pageNo, Integer pageSize, String chiefOpenId) {
		return memberRepository.findFansPage(pageNo, pageSize, chiefOpenId);
	}

	@Override
	public Member executeUpdateMember(Member member) {
		memberRepository.update(member);
		return member;
	}

	@Override
	public Page<Member> findMemberPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Date startTime, Date endTime, Integer vipLevel, String partnerOpenId) {
		return memberRepository.findMemberPage(pageNo, pageSize, searchParams, sortParam, sortType, startTime, endTime,
				vipLevel, partnerOpenId);
	}

	@Override
	public Member executeAddMember(String openId, String chiefOpenId, String chiefCardNo, String name, String avatar,
			String weixin) {
		Member member = memberRepository.findMemberByOpenId(openId);
		Member chiefMember = memberRepository.findMemberByOpenId(chiefOpenId);
		if (null == chiefMember) {
			chiefMember = new Member();
			chiefMember.setOpenId("otFms0ib9eecFHRe2pa98b-Bfjh0");
			chiefMember.setCardNo("73464473");
		}
		member = new Member();
		member.setName(name);
		member.setAvatar(avatar);
		member.setFollowStatus(Member.FOLLOW_STATUS_YES);
		member.setOpenId(openId);
		member.setCardNo(UUID.randomUUID().toString().replaceAll("-", ""));
		member.setChiefOpenId(chiefMember.getOpenId());
		member.setChiefCardNo(chiefMember.getCardNo());
		member.setFlow(0L);
		member.setVipLevel(Member.VIP_LEVEL_FANS);
		member.setExpireDate(DateUtils.getDateFromDateStr("2117-09-09 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		member.setRegisterTime(new Date());
		member.setStatus(Member.STATUS_USE);
		member.setFansCounts(0L);
		memberRepository.save(member);
		int lenth = 8 - String.valueOf(member.getId()).length();
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < lenth; i++) {
			sb.append(String.valueOf(StrUtils.getRandomInt()));
		}
		member.setCardNo(sb.toString() + String.valueOf(member.getId()));
		memberRepository.update(member);

		WeiBiAccount weiBiAccount = new WeiBiAccount();
		weiBiAccount.setOpenId(member.getOpenId());
		weiBiAccount.setCardNo(member.getCardNo());
		weiBiAccount.setBalance(new BigDecimal(100));
		weiBiAccount.setBalanceBegin(new BigDecimal(100));
		weiBiAccount.setUpdateTime(new Date());
		weiBiAccount.setEncryption(MD5Utils.generate(weiBiAccount.getBalance().doubleValue() * 10000 + ""));
		weiBiAccountRepository.save(weiBiAccount);
		WeiBiAccountTrace weiBiAccountTrace = new WeiBiAccountTrace();
		weiBiAccountTrace.setWeiBiAccountId(weiBiAccount.getId());
		weiBiAccountTrace.setChangeType(ChangeType.SYS_GIVE_ADD);
		weiBiAccountTrace.setBeforeValue(BigDecimal.ZERO);
		weiBiAccountTrace.setChangeValue(new BigDecimal(100));
		weiBiAccountTrace.setAfterValue(new BigDecimal(100));
		weiBiAccountTrace.setChangeTime(new Date());
		weiBiAccountTrace.setChangeDate(Integer.valueOf(DateUtils.getDateStr(new Date(), "yyyyMMdd")));
		weiBiAccountTraceRepository.save(weiBiAccountTrace);

		JinBiAccount jinBiAccount = new JinBiAccount();
		jinBiAccount.setOpenId(member.getOpenId());
		jinBiAccount.setCardNo(member.getCardNo());
		jinBiAccount.setBalance(BigDecimal.ZERO);
		jinBiAccount.setBalanceBegin(BigDecimal.ZERO);
		jinBiAccount.setUpdateTime(new Date());
		jinBiAccount.setEncryption(MD5Utils.generate(jinBiAccount.getBalance().doubleValue() * 10000 + ""));
		jinBiAccountRepository.save(jinBiAccount);
		return member;
	}

	@Override
	public Member executeUpdateMember(String openId, String mobile, String weixin, Integer status,
			Integer followStatus) {
		Member member = memberRepository.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_MEMBER_NOT_EXIST);
		}
		if (StrUtils.isNotNULL(mobile)) {
			member.setMobile(mobile);
		}
		if (StrUtils.isNotNULL(weixin)) {
			member.setWeixin(weixin);
		}
		if (null != status) {
			member.setStatus(status);
		}
		if (null != followStatus) {
			member.setFollowStatus(followStatus);
		}
		memberRepository.update(member);
		return member;
	}

	@Override
	public Member executeUpdateMember(String openId, String name, String avatar) {
		Member member = memberRepository.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_MEMBER_NOT_EXIST);
		}
		if (StrUtils.isNotNULL(name)) {
			member.setName(name);
		}
		if (StrUtils.isNotNULL(avatar)) {
			member.setAvatar(avatar);
		}
		memberRepository.update(member);
		return member;
	}

	@Override
	public List<Member> findMembersForRank(int limitCounts) {
		return memberRepository.findMembersForRank(limitCounts);
	}

	@Override
	public List<Member> findMembersPage(Integer pageNo, Integer pageSize) {
		return memberRepository.findMembersPage(pageNo, pageSize);
	}

	@Override
	public List<Member> findPartnerAgentPage(Integer pageNo, Integer pageSize, String partnerOpenId) {
		return memberRepository.findPartnerAgentPage(pageNo, pageSize, partnerOpenId);
	}

	@Override
	public Member findByMobile(String mobile) {
		return memberRepository.findByMobile(mobile);
	}

	@Override
	public List<Member> findFans(String chiefOpenId) {
		return memberRepository.findFans(chiefOpenId);
	}

	@Override
	public List<Member> findMembersPage(Integer pageNo, Integer pageSize, List<Integer> levelList) {
		return memberRepository.findMembersPage(pageNo, pageSize, levelList);
	}

}
